# coding=utf-8
import json

import requests

'''
maokoulingClass.py 说明
            class maokouling_ONE():  我的猫口令配置信息
            |
            #根据id获得商品的高佣长链接
            def tblink_get_gaoyong(config):
            |
'''


# 每个人配置一个请求信息，要求账户设置白名单
# pid（mm_账号id_推广渠道id_推广位id）
# mm_memberId_siteId_adzoneId
# mm_28964573_303450418_82851750421
# 每个人配置一个请求信息，要求账户设置白名单

class maokouling_ONE():
    # 读取账号信息,如果user是1,是shenhuawade的账号,如果是2是小白的账号
    # with open('weixin.json', 'r', encoding='utf-8') as f:
    #     userjsons = json.load(f)
    #     print(userjsons)
    # accuntUser = userjsons["user"]

    '''shenhuawade账号!!!----Start'''
    itemid = ''
    userid = ''  # 店铺的userid
    pid = 'mm_28964573_303450418_82851750421'  # 淘宝联盟密令 '6e39417a-bc77-3d4d-c2ae-0a51f37d2574'   # 喵口令密令
    apkey = '6e39417a-bc77-3d4d-c2ae-0a51f37d2574'  # 喵口令密令
    tbname = 'shenhuawade'
    tpwdcode = '￥OxXGby1GRvh￥'  # 任意淘口令
    tpwd = '1'  # 是否生成淘口令，1为生成，不填则不生成
    shorturl = '1'  # 是否需要短链接，1为生成，不填则不生成
    extsearch = '1'  # 官方查不到券时是否去第三方平台查询，1为启用
    tpwdpic = ''  # 商品图片链接，用于打开口令时显示，不填写默认为商品主图
    itemTbkLink = ''



# 淘宝 SLICK链接拿商品id
def sclikto_id(config, slickUrl):
    try:
        url = f'http://api.web.21ds.cn/taoke/sclicktoid?apkey={config.apkey}&sclickurl={slickUrl}'
        print(url)
        itemJson = requests.get(url, timeout=100)
        print(itemJson.json())
        if itemJson.json()['code'] != 200:
            print("#淘宝 SLICK链接拿商品id sclikto_id ----ID-GET-link--API状态码错误")
            return None
        else:
            itemid = itemJson.json()['data']
            return itemid
    except Exception as e:
        print(e)
        print('淘宝 SLICK链接拿商品id出错')
        return None


# 根据id获得商品的高佣长链接
def tblink_get_gaoyong(config):
    try:
        url = f'http://api.web.21ds.cn/taoke/doItemHighCommissionPromotionLink?apkey={config.apkey}&itemid={config.itemid}&pid={config.pid}&tbname={config.tbname}'
        # url = f' https://api.open.21ds.cn/apiv1/getitemgyurl?apkey={config.apkey}&itemid={config.itemid}&pid={config.pid}&tbname={config.tbname}'
        print(url)
        html = requests.get(url, timeout=5)
        # print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:

            print("再tblink_get_gaoyong ----ID-GET-link--API状态码错误")
            return None
        else:
            itemTbkLink = itemJson['data']['item_url']
            # print(itemTbkLink)
            config.itemTbkLink = itemTbkLink
            return itemTbkLink
    except Exception as e:
        print(e)
        print('ID-GET-link白名单限制')
        return None

    # 通过淘宝id查出来店铺的userid


# 根据淘宝商品id 获得商铺的userid，传给userid_to_shopTBK_link获得店铺tbk链接
def item_to_SELLID(config):
    try:
        # url =f'https://api.open.21ds.cn/apiv1/getiteminfo?apkey=6e39417a-bc77-3d4d-c2ae-0a51f37d2574&itemid=568706761354'
        url = f'http://api.web.21ds.cn/taoke/getItemInfo?apkey={config.apkey}&itemid={config.itemid}'
        print(url)
        html = requests.get(url, timeout=5)
        print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:
            print("ID--GET--SHOPID--API状态码错误")
            return None
        else:
            shopid = itemJson['data']['seller_id']
            print(shopid)
            return shopid
    except Exception as e:
        print(e)
        print('ID--GET--SHOPID--白名单限制')
        return None


# 根据店铺的id获得推广商铺淘宝客链接
def userid_to_shopTBK_link(config):
    try:
        # url =f'https://api.open.21ds.cn/apiv1/doShopCovert?apkey=6e39417a-bc77-3d4d-c2ae-0a51f37d2574&pid=mm_123456_456789_789132&user_ids=123456,789456&tbname=xxxxxx'
        url = f'http://api.web.21ds.cn/taoke/doShopCovert?apkey={config.apkey}&pid={config.pid}&user_ids={config.userid}&tbname={config.tbname}'
        # print(config.userid)
        print(url)
        html = requests.get(url, timeout=5)
        # print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:
            print("SHOPID--GET--LINK--API状态码错误")
            return None
        else:
            shopLINK = itemJson['data'][0]['click_url']
            # print(itemTbkLink)
            return shopLINK
    except Exception as e:
        print(e)
        print('SHOPID--GET--LINK--白名单限制,可能是淘宝商品参加推广了,但是淘宝店铺没有参加推广,为防止空白,替换链接为商品淘宝客链接')
        return config.itemTbkLink


if __name__ == '__main__':
    abc = maokouling_ONE()

    abc.itemid = 45074712601

    abc.userid = item_to_SELLID(abc)
    # print(tblink_get_gaoyong(abc))
    abc.itemTbkLink = tblink_get_gaoyong(abc)
    print(tblink_get_gaoyong(abc))
    print(userid_to_shopTBK_link(abc))
